<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";
 require "../tree/class_drawer_svg.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";

 require "./tool_alignement.php";

function rep2svg($_rep,$_title){
  $min_y = min($_rep);
  $max_y = max($_rep);

  $tool_graph = new Tool_pear_graph(600,175);
  $tool_graph->__set_interval_x(1);
  $tool_graph->__set_max_y($max_y+100);
  $tool_graph->__set_min_y($min_y-100);
  $tool_graph->__add_array_x(array_keys($_rep));
  $tool_graph->__add_array_y(array_values($_rep));
  $tool_graph->__add_plot_color("#FF0000");
  $tool_graph->__add_title($_title);
  $graph = $tool_graph->line_color();
  ob_start();
  $graph->done();
  $out_svg = ob_get_contents();
  ob_clean();
/*  $res = mb_ereg_replace("<\?[^?]+\?>\n<\![^>]+>\n","",$out_svg); */
  $res = $out_svg;
  return $res;
}

function tree2svg_color_mfm($_tree){
  $array_mfm = tool_tree::structure2array_mfm($_tree);
  asort($array_mfm,SORT_NUMERIC);
  $mfm_rename = array();
  $i = 0;
  foreach($array_mfm as $mfm=>$cpt) $mfm_rename[$mfm] = $i++;
  $tree_renamed = tool_tree::structure_mfm2structure_renamed($_tree,$mfm_rename);
  $options_rename = array('print'=>'color_value');
  $f = $tree_renamed->__get_forest();
  $svg = tool_forest::forest2svg($f,$options_rename);
  return $svg;
}


$dir = "./corpus/test/";
$file = "celex_IP-05-1510.cs.xml";
$file = "celex_IP-09-351.fr.xml";
$corpus = tool_corpus::get_corpus_regexp($dir,$id_europa.$file);
$document = $corpus->__get_doc(0);


//
// Calcul de signatures et de structures
//

$structure = $document->__get_structure();
$signature = $structure->__get_signature();
$signature->__recalc_idn();
$svg_structure = tool_tree::tree2svg($structure);
$svg_signature = tool_tree::tree2svg($signature);

//
// Calcul d'évolution des nombres de caractères
//

$rep_structure = tool_tree::structure2strlen($structure);
$deriv_rep_structure = tool_maths::get_deriv($rep_structure);
$cumul_struct = tool_maths::get_cumulative_percent_vector($rep_structure);
$cumul_struct = tool_array::round($cumul_struct,2);

$prep_struct = tool_array::printr($rep_structure);
$prep_deriv_struct = tool_array::printr($deriv_rep_structure);
$pcumul_struct = tool_array::printr($cumul_struct);
$svg_rep_struct = rep2svg($rep_structure,"évolution structure");
$svg_deriv_struct = rep2svg($deriv_rep_structure,"vitesse évolution structure");

$rep_signature = tool_tree::structure2strlen($signature);
$deriv_rep_signature = tool_maths::get_deriv($rep_signature);
$cumul_sign = tool_maths::get_cumulative_percent_vector($rep_signature);
$cumul_sign = tool_array::round($cumul_sign,2);

$prep_sign = tool_array::printr($rep_signature);
$prep_deriv_sign = tool_array::printr($deriv_rep_signature);
$pcumul_sign = tool_array::printr($cumul_sign);
$svg_rep_sign = rep2svg($rep_signature,"évolution signature");
$svg_deriv_sign = rep2svg($deriv_rep_signature,"vitesse évolution signature");

$forest_signature = $signature->__get_forest();
$options = array('print'=>'color_deep');
$info_svg_forest = tool_forest::forest2svg($forest_signature,$options);

$t_struct = tree2svg_color_mfm($structure);
$t_sign = tree2svg_color_mfm($signature);


$print_doc = "
  <div style=\"height:300px; overflow:auto; font-size:0.6em;\">
  $document
  </div>
";


$header_xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
                      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\"
      xmlns:svg=\"http://www.w3.org/2000/svg\">
";

tool_files::file_write("./out/struct.svg",$t_struct['str']);
tool_files::file_write("./out/sign.svg",$t_sign['str']);
tool_files::file_write("./out/rep_sign.svg",$svg_rep_sign);
tool_files::file_write("./out/deriv_sign.svg",$svg_deriv_sign);
tool_files::file_write("./out/rep_struct.svg",$svg_rep_struct);
tool_files::file_write("./out/deriv_struct.svg",$svg_deriv_struct);

/*

$svg_rep_struct
$svg_deriv_struct

$svg_rep_sign
$svg_deriv_sign

 </html>
";
*/
?>
